package JZOffer;

import java.util.ArrayList;

/**
 * @author Lw中
 * @date 2021/8/27 22:03
 */
public class Jz57II {

    // Jz57 - II. 和为s的连续正数序列
    public int[][] findContinuousSequence(int target) {

        // 方法一：暴力拆解
        ArrayList<int[]> container = new ArrayList<>();
        // 因为题目要求序列元素个数大于2，所以limit为target/2
        int limit = (target - 1) / 2, sum = 0;
        // 使用双重循环进行遍历出所有的情况
        for (int i = 1; i <= limit; i++) {
            for (int j = i; ; j++) {
                sum += j;
                if (sum > target) {
                    sum = 0;
                    break;
                }
                else if (sum == target) {
                    int[] res = new int[j - i + 1];
                    for (int k = i; k <= j; k++) {
                        res[k - i] = k;
                    }
                    container.add(res);
                    sum = 0;
                    break;
                }
            }
        }
        return container.toArray(new int[container.size()][]);
    }

}
